<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="modules.html" />
<link rel="prev" href="custominterp.html" />
<link rel="parent" href="lib.html" />
<link rel="next" href="module-rexec.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>28. Restricted Execution </title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="27.2 codeop  "
  href="module-codeop.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="Python library Reference"
  href="lib.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="28.1 rexec  "
  href="module-rexec.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-codeop.html">27.2 codeop  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="lib.html">Python Library Reference</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-rexec.html">28.1 rexec  </a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION0030000000000000000000"></a><a name="restricted"></a>
<br>
28. Restricted Execution 
</h1>

<p>
<div class="warning"><b class="label">Warning:</b>

   In Python 2.3 these modules have been disabled due to various known
   and not readily fixable security holes.  The modules are still
   documented here to help in reading old code that uses the
   <tt class="module">rexec</tt> and <tt class="module">Bastion</tt> modules.
</div>

<p>
<em>Restricted execution</em> is the basic framework in Python that allows
for the segregation of trusted and untrusted code.  The framework is based on the
notion that trusted Python code (a <em>supervisor</em>) can create a
``padded cell' (or environment) with limited permissions, and run the
untrusted code within this cell.  The untrusted code cannot break out
of its cell, and can only interact with sensitive system resources
through interfaces defined and managed by the trusted code.  The term
``restricted execution'' is favored over ``safe-Python''
since true safety is hard to define, and is determined by the way the
restricted environment is created.  Note that the restricted
environments can be nested, with inner cells creating subcells of
lesser, but never greater, privilege.

<p>
An interesting aspect of Python's restricted execution model is that
the interfaces presented to untrusted code usually have the same names
as those presented to trusted code.  Therefore no special interfaces
need to be learned to write code designed to run in a restricted
environment.  And because the exact nature of the padded cell is
determined by the supervisor, different restrictions can be imposed,
depending on the application.  For example, it might be deemed
``safe'' for untrusted code to read any file within a specified
directory, but never to write a file.  In this case, the supervisor
may redefine the built-in <tt class="function">open()</tt> function so that it raises
an exception whenever the <var>mode</var> parameter is <code>'w'</code>.  It
might also perform a <tt class="cfunction">chroot()</tt>-like operation on the
<var>filename</var> parameter, such that root is always relative to some
safe ``sandbox'' area of the filesystem.  In this case, the untrusted
code would still see an built-in <tt class="function">open()</tt> function in its
environment, with the same calling interface.  The semantics would be
identical too, with <tt class="exception">IOError</tt>s being raised when the
supervisor determined that an unallowable parameter is being used.

<p>
The Python run-time determines whether a particular code block is
executing in restricted execution mode based on the identity of the
<code>__builtins__</code> object in its global variables: if this is (the
dictionary of) the standard <tt class="module"><a href="module-builtin.html">__builtin__</a></tt> module,
the code is deemed to be unrestricted, else it is deemed to be
restricted.

<p>
Python code executing in restricted mode faces a number of limitations
that are designed to prevent it from escaping from the padded cell.
For instance, the function object attribute <tt class="member">func_globals</tt> and
the class and instance object attribute <tt class="member">__dict__</tt> are
unavailable.

<p>
Two modules provide the framework for setting up restricted execution
environments:

<p>
<table class='synopsistable' valign='baseline'>
  <tr class='oddrow'>
      <td><b><tt class='module'><a href='module-rexec.html'>rexec</a></tt></b></td>
      <td>&nbsp;</td>
      <td class='synopsis'>Basic restricted execution framework.</td></tr>
  <tr><td><b><tt class='module'><a href='module-Bastion.html'>Bastion</a></tt></b></td>
      <td>&nbsp;</td>
      <td class='synopsis'>Providing restricted access to objects.</td></tr>
</table>

<br>
<p>
<div class="seealso">
  <p class="heading">See Also:</p>

  <dl compact="compact" class="seetitle">
    <dt><em class="citetitle"><a href="http://grail.sourceforge.net/"
        >Grail Home Page</a></em></dt>
    <dd>Grail, an Internet browser written in Python, uses these
            modules to support Python applets.  More
            information on the use of Python's restricted execution
            mode in Grail is available on the Web site.</dd>
  </dl>
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="27.2 codeop  "
  href="module-codeop.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="Python library Reference"
  href="lib.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="28.1 rexec  "
  href="module-rexec.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-codeop.html">27.2 codeop  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="lib.html">Python Library Reference</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-rexec.html">28.1 rexec  </a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
